package com.bigger.arch.app.repository.dao.impl;

import com.bigger.arch.app.repository.util.MyBatisUtil;
import com.bigger.arch.app.repository.dao.UserDao;
import com.bigger.arch.app.repository.model.UserModel;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("MyUserDaoImpl")
public class MyUserDaoImpl implements UserDao {

    public UserModel getItemById(Integer id) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            //UserModel userModel = (UserModel) sqlSession.selectOne("userNS.getItemById1", id);
            UserModel userModel = (UserModel) sqlSession.selectOne("userNS.getItemById", id);
            return userModel;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public UserModel getItemByName(String name) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            UserModel userModel = (UserModel) sqlSession.selectOne("userNS.getItemByName", name);
            return userModel;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public List<UserModel> getItems() {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            return sqlSession.selectList("userNS.getItems");
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public List<UserModel> getLimitItems(int offset, int limitSize) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("offset", offset);
            parameters.put("limitSize", limitSize);

            Map<Integer, UserModel> userModelMap = sqlSession.selectMap("userNS.getLimitItems", parameters, "id");

            return sqlSession.selectList("userNS.getLimitItems", parameters);
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public List<UserModel> serachItems(UserModel serachModel, int offset, int limitSize) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            Map<String, Object> parameters = new HashMap<String, Object>();
            if (serachModel != null) {
                if (serachModel.getName() != null) {
                    serachModel.setName("%" + serachModel.getName() + "%");
                }
            }
            parameters.put("serachModel", serachModel);
            parameters.put("offset", offset);
            parameters.put("limitSize", limitSize);

            return sqlSession.selectList("userNS.serachItems", parameters);
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public List<UserModel> searchUsersAndArticles(String userName, String title, int offset, int limitSize) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            Map<String, Object> parameters = new HashMap<String, Object>();
            if (userName != null) {
                userName="%" + userName + "%";
            }
            if (title != null) {
                title="%" + title + "%";
            }
            parameters.put("userName", userName);
            parameters.put("title", title);
            parameters.put("offset", offset);
            parameters.put("limitSize", limitSize);

            return sqlSession.selectList("userNS.searchUsersAndArticles", parameters);
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public BigInteger getCount() {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            return sqlSession.selectOne("userNS.getCount");
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public Integer insertItem(UserModel userModel) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            int result = sqlSession.insert("userNS.insertItem", userModel);
            sqlSession.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            return 0;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public Integer updateItem(UserModel userModel) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            int result = sqlSession.update("userNS.updateItem", userModel);
            sqlSession.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            return 0;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public Integer updateBalance(UserModel userModel) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            int result = sqlSession.update("userNS.updateBalance", userModel);
            sqlSession.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            return 0;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public Integer deleteItem(UserModel userModel) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            int result = sqlSession.update("userNS.deleteItem", userModel);
            sqlSession.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            return 0;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }

    public Integer deleteItems(List<Integer> ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            int result = sqlSession.update("userNS.deleteItems", ids);
            sqlSession.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            return 0;
        } finally {
            MyBatisUtil.closeSqlSession();
        }
    }
}
